<?php
class Admin_Model_Articles extends My_Model_Db
{
	protected $_mainTable = 'articles';

	protected $_idFieldName = 'p_articles';

	public function getCollection($numVente)
	{
	    $select = $this->getAdapter()->select()
	    ->from(
            $this->_mainTable,
            array('num_article', 'estimation', 'p_articles', 'desc_longue', 'numero', 'sous', 'adjuge', 'estimation', 'caisse')
        )
        ->join('categories', 'categories.p_categories = articles.f_categories', array('nom'))
        ->join('series', 'articles.f_series = series.p_series', array('titre'))
        ->where('articles.deleted = ?', 0)
        ->where('articles.f_ventes = ?', $numVente)
        ->order(array('caisse'));
	    return $select;
	    //return $this->getAdapter()->fetchAll($select);
	}

	public function getArticle($id)
	{
	    $select = $this->getAdapter()->select()->from($this->_mainTable)
	    ->where($this->_idFieldName . ' = ?', $id);
	    return $this->getAdapter()->fetchRow($select);
	}

	public function getMaxId()
	{
	    $select = $this->getAdapter()->select()->from($this->_mainTable, array('max_id' => 'MAX(' . $this->_idFieldName . ')'))
        ->limit(1);
	    return $this->getAdapter()->fetchOne($select);
	}

	public function insertArticleOffrant($data)
	{
	    $this->getAdapter()->insert('articles_offrants', $data);
	}

	public function getClients()
	{
	    $select = $this->getAdapter()->select()
	    ->from('clients', array('prenom', 'nom', 'p_clients'))
	    ->where('vendeur = ?', 1)
	    ->where('deleted = ?', 0)
	    ->order(array('nom', 'prenom'));

	    return $this->getAdapter()->fetchAll($select);
	}

	public function getAcheteur($clientAcheteur, $numVente)
	{
	    $select = $this->getAdapter()->select()
	    ->from('clients', array('prenom', 'nom'))
	    ->join('clients_ventes', 'clients_ventes.f_clients = clients.p_clients', array('num_client'))
	    ->where('p_clients = ?', $clientAcheteur ? $clientAcheteur : 0)
	    ->where('deleted = ?', 0)
	    ->where('f_ventes = ?', $numVente)
	    ->order(array('nom', 'prenom'));

	    return $this->getAdapter()->fetchRow($select);
	}

	public function getSeries()
	{
	    $select = $this->getAdapter()->select()
	    ->from('series', array('*'))
	    ->where('deleted = ?', 0)
	    ->where('archive = ?', 0)
	    ->order(array('titre'));

	    return $this->getAdapter()->fetchAll($select);
	}

	public function getCategories($numVente)
	{
	    $select = $this->getAdapter()->select()
	    ->from('categories', array('nom', 'p_categories'))
	    ->where('deleted = ?', 0)
	    ->where('f_ventes = ?', $numVente)
	    ->order(array('ordre', 'nom'));

	    return $this->getAdapter()->fetchAll($select);
	}

	public function getVentes()
	{
	    $select = $this->getAdapter()->select()
	    ->from('ventes', array('dtvente', 'p_ventes', 'titre'))
	    ->where('deleted = ?', 0)
	    ->order(array('dtvente'));

	    return $this->getAdapter()->fetchAll($select);
	}

	public function deleteOffrants($id)
	{
	    $where = $this->getAdapter()->quoteInto('f_articles = ?', $id);
	    $this->getAdapter()->delete('articles_offrants', $where);
	    return $this;
	}

	public function searchArticle($numArticle, $numVente)
	{
        $select = $this->getAdapter()->select()
        ->from($this->_mainTable, array($this->_idFieldName))
        ->where('num_article = ?', $numArticle)
        ->where('f_ventes = ?', $numVente);

        return $this->getAdapter()->fetchOne($select);
	}

	public function getCsvCollection($numVente)
	{
	    $fieldSelect = array( 'num_article', 'estimation', 'desc_longue', 'numero', 'sous', 'adjuge', 'caisse', 'reserve');
	    $select = $this->getAdapter()->select()
	    ->from($this->_mainTable, $fieldSelect)
	    ->join('categories', 'categories.p_categories = articles.f_categories', array('nom'))
	    ->join('series', 'articles.f_series = series.p_series', array('titre'))
	    ->join('clients', 'clients.p_clients = articles.f_clients_deposant', array('lenom' => 'nom'))
	    ->where('articles.deleted = ?', 0)
	    ->where('articles.f_ventes = ?', $numVente)
	    ->order(array('categories.ordre', 'categories.nom', 'series.titre', 'articles.numero', 'sous', 'articles.desc_longue'))
	    ;
	    return $this->getAdapter()->fetchAll($select);
	}

	public function checkRenum($numVente)
	{
	    $select = $this->getAdapter()->select()->from('ventes', array('renum'))->where('p_ventes = ?', $numVente);
	    return $this->getAdapter()->fetchOne($select);
	}

	public function getArticleOffrant($id, $offset = 0)
	{
        $select = $this->getAdapter()->select()
        ->from('articles_offrants')
        ->where('f_articles = ?', $id)
        ->limit(1, $offset);

        return $this->getAdapter()->fetchRow($select);
	}

	public function getClientsOffrant($numVente)
	{
	    $select = $this->getAdapter()->select()
	    ->from('clients', array('prenom', 'nom', 'p_clients'))
	    ->join('clients_ventes', 'clients_ventes.f_clients = clients.p_clients', array('num_client'))
	    ->where('clients.acheteur = ?', 1)
	    ->where('clients.deleted = ?', 0)
	    ->where('clients_ventes.f_ventes = ?', $numVente)
	    ->order('num_client', 'nom', 'prenom');

	    return $this->getAdapter()->fetchAll($select);
	}
}